// pages/post/post.js
import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast'
var js_data = require('../../data/post/post_fixed.js')
var posts_js_data = js_data.data
var collected_status_js_daya = js_data.collected_status
const app = getApp()
Page({

  data: {
    // 5大功能模块对应的数据和样式
    function_btn: [
      {
        icon: 'icon-wode1',
        text: '我的空间',
        function: -1
      },
      {
        icon: 'icon-shequ-active',
        text: '邻里之间',
        function: 0
      },
      {
        icon: 'icon-xunhuan',
        text: '闲物置换',
        function: 1
      },
      {
        icon: 'icon-classrichangqingjie',
        text: '家政服务',
        function: 2
      },
      {
        icon: 'icon-tousujianyi',
        text: '建言献策',
        function: 3
      }
    ],
    movable_x: null,
    movable_y: null,
    posts_list:[],
    pageSize: 15,
    pageNum: 1,
    total: null
  },

  setMovableViewPosition: function(){
    // 获取屏幕宽高，设置悬浮新建按钮的初始位置
    var windowWidth = wx.getSystemInfoSync().windowWidth
    var windowHeight = wx.getSystemInfoSync().windowHeight
    // console.log(windowHeight)
    var movable_x = String(windowWidth / 2 - 20) + 'px'
    var movable_y = String(windowHeight - 50) + 'px'
    // x="140px" y="450px"
    this.setData({
      movable_x: movable_x,
      movable_y: movable_y
    })
  },

  onLoad: function (options) {
    // 读取屏幕宽高,动态调整悬浮按钮的位置
    // this.setMovableViewPosition()
    // app.sendGetRequest('/bbs/list/?deleteflag=0',(res) => {
    //   this.setData({
    //     posts_list:res.rows
    //   })
    // })
    this._onLoad()
  },
  _onLoad:function(){
    this.setMovableViewPosition()
    
    var param = {
      pageSize: this.data.pageSize,
      pageNum: this.data.pageNum,
      deleteflag: 0 
    }
    console.log(param)
    app.sendGetRequestWithData('/bbs/list/', param,(res) => {
      this.setData({
        posts_list:res.rows,
        total: res.total
      })
    })
  },
  onPullDownRefresh:function(){
   
    this._onLoad()
    wx.stopPullDownRefresh()
  },
  onBriefPostTap: function (e) {
    // 获取被点击帖子的post_id
    var articleid = e.currentTarget.dataset.post_id
    console.log("before",articleid)
    wx.navigateTo({
      url: './post_content/post_content?articleid=' + articleid,
    })
  },

  onFunctionTap: function (e){
    var type = e.currentTarget.dataset.function
    if(type==-1){
      wx.navigateTo({
        url: './my_space/my_space',
      })
    }else{
      wx.navigateTo({
        url: './post_list/post_list?category=' + type,
      })
    }
  },
  
  create_post: function (e){
    wx.showActionSheet({
      itemList: ['邻里之间', '闲物置换', '家政服务', '建言献策'],
      success: function(res) {
          // console.log(res.tapIndex)
          var category = String(res.tapIndex)
          wx.navigateTo({
            url: './create_post/create_post?category=' + category,
          })
      },
      fail: function(res) {
          console.log(res.errMsg)
      }
    })
  },
  scrollToLower: function(e){
    var nextPage = this.data.pageNum + 1;
    if(nextPage * this.data.pageSize - this.data.total > this.data.pageSize){
      Toast('后面没有了')
      return
    }
    var oldList = this.data.posts_list
    var param = {
      pageSize: this.data.pageSize,
      pageNum: this.data.pageNum + 1,
      deleteflag: 0 
    }
    app.sendGetRequestWithData('/bbs/list/', param,(res) => {
      var after = oldList.concat(res.rows)
      this.setData({
        posts_list: after,
        total: res.total,
        pageNum: nextPage
      })
    })
    
  }
})